//
//  CSNateSessionController.m
//  CairnStory
//  www.cairnstory.com
//
//  Created by saturna on 11. 3. 22..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CSNateSessionController.h"
#import "CSNateSessionDelegate.h"
#import "CSNateOAuthViewController.h"
#import "CSNateOAuthResult.h"
#import "CSNateRequest.h"


#pragma mark -
#pragma mark CSNateSessionController (PrivateMethods)

@interface CSNateSessionController (PrivateMethods)

- (void)requestCLogProfile;

@end




#pragma mark -
#pragma mark constants define


// Nate OpenAPI consumer key, consuer secret 발급신청
// --> http://devsquare.nate.com/openApi/registerConsumerKey
#define kNateOAuthConsumerKey @"nate-open-api-consumer-key"
#define kNateOAuthConsumerSecret @"nate-open-api-consumer-secret"

// callback url은 임의의 url을 사용한다.
// (존재하지 않는 가상 url도 가능)
#define kNateOAuthCallbackURLString @"http://www.callback.com/nate/callback"


#define kNateTagCLogProfile 1


#pragma mark -
#pragma mark CSNateSessionController

@implementation CSNateSessionController

@synthesize delegate = m_delegate;
@synthesize nateTag = m_nateTag;



#pragma mark -
#pragma mark +++++

- (void)dealloc
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	[super dealloc];
}


- (void)login
{
	//if (DEBUG) CSLog(@"====> login");
	[m_delegate nateSessionController:self
					  didStartWithTag:m_nateTag];

	CSNateOAuthViewController* aNateOAuthViewController = [[CSNateOAuthViewController alloc]
														 initWithConsumerKey:kNateOAuthConsumerKey
														 andConsumerSecret:kNateOAuthConsumerSecret
														 andCallback:kNateOAuthCallbackURLString
														 andDelegate:self];
	aNateOAuthViewController.navigationItem.title = @"Nate Login";
	[m_delegate.navigationController pushViewController:aNateOAuthViewController animated:NO];
	[aNateOAuthViewController release];
}


#pragma mark -
#pragma mark CSNateOAuthViewDelegate

- (void)nateOAuthViewController:(CSNateOAuthViewController*)controller
		 didLoginWithAuthResult:(CSNateOAuthResult*)oauthResult
{
	if (DEBUG) CSLog(@"=====> %@", oauthResult);
	m_delegate.appDelegate.nateOAuthResult = oauthResult;

	[self requestCLogProfile];
//	[m_delegate nateSessionController:self
//					  didLoginWithTag:m_nateTag
//					   withAuthResult:oauthResult];
//	[m_delegate nateSessionController:self
//					 didFinishWithTag:m_nateTag];
}

- (void)nateOAuthViewController:(CSNateOAuthViewController*)controller
		didRejectWithAuthResult:(CSNateOAuthResult*)authResult
{
	m_delegate.appDelegate.nateOAuthResult = nil;
	[m_delegate nateSessionController:self
					 didRejectWithTag:m_nateTag
					   withAuthResult:authResult
						  withMessage:@"Nate/Cyworld login rejected!!"];
	[m_delegate nateSessionController:self
					 didFinishWithTag:m_nateTag];
}

- (void)nateOAuthViewControllerDidCancel:(CSNateOAuthViewController*)controller
{
	m_delegate.appDelegate.nateOAuthResult = nil;
	[m_delegate nateSessionController:self
					 didCancelWithTag:m_nateTag];
	[m_delegate nateSessionController:self
					 didFinishWithTag:m_nateTag];
}

- (void)nateOAuthViewController:(CSNateOAuthViewController*)controller
			   didFailWithError:(NSError*)error
{
	m_delegate.appDelegate.nateOAuthResult = nil;
	[m_delegate nateSessionController:self
					   didFailWithTag:m_nateTag
						  withMessage:@"nate login failed!!"
							withError:error];
	[m_delegate nateSessionController:self
					 didFinishWithTag:m_nateTag];
}





#pragma mark -
#pragma mark CSNateRequestDelegate


- (void)nateRequest:(CSNateRequest*)request
	didStartWithTag:(NSUInteger)nateTag
{
	// do nothing
}


- (void)nateRequest:(CSNateRequest*)request
  didSuccessWithTag:(NSUInteger)nateTag
	 withOutputType:(CSNateRequestOutputType)outputType
		 withResult:(id)result
{
	if (DEBUG) CSLog(@"===> result : %@", result);
	if (kNateTagCLogProfile == nateTag)
	{
		NSDictionary* resultDic = (NSDictionary*)result;
		NSInteger errorCode = [[[resultDic objectForKey:@"rcode"] substringFromIndex:3] intValue];
		if (errorCode == 0)
		{
			NSArray* allKey = [resultDic allKeys];
			for (NSString* key in allKey)
			{
				if ([key isEqualToString:@"rcode"] == NO && [key isEqualToString:@"rmsg"] == NO)
				{
					[m_delegate.appDelegate.nateOAuthResult setCLogProfile:[resultDic objectForKey:key] forKey:key];
				}
			}
		}
		else
		{
			if (DEBUG) CSLog(@"===> rcode : %@, rmsg : %@",
							 [resultDic objectForKey:@"rcode"],
							 [resultDic objectForKey:@"rmsg"]);
		}

		[m_delegate nateSessionController:self
						  didLoginWithTag:m_nateTag
						   withAuthResult:m_delegate.appDelegate.nateOAuthResult];
	}
	else
	{
		CSAssert1(FALSE, @"unsupported nate tag [%d]", nateTag);
	}
}


- (void)nateRequest:(CSNateRequest*)request
	 didFailWithTag:(NSUInteger)nateTag
		withMessage:(NSString*)alertMessage
		  withError:(NSError*)error
{
	if (DEBUG) CSLog(@"===> error : %@", error);
	if (kNateTagCLogProfile == nateTag)
	{
		[m_delegate nateSessionController:self
						  didLoginWithTag:m_nateTag
						   withAuthResult:m_delegate.appDelegate.nateOAuthResult];
	}
	else
	{
		CSAssert1(FALSE, @"unsupported nate tag [%d]", nateTag);
	}
}

- (void)nateRequest:(CSNateRequest*)request
   didFinishWithTag:(NSUInteger)nateTag
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}
	if (kNateTagCLogProfile == nateTag)
	{
		[m_delegate nateSessionController:self
						 didFinishWithTag:m_nateTag];
	}
	else
	{
		CSAssert1(FALSE, @"unsupported nate tag [%d]", nateTag);
	}
}




#pragma mark -
#pragma mark CSNateSessionController (PrivateMethods)

- (void)requestCLogProfile
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	m_nateRequest = [[CSNateRequest alloc] initWithNateOAuthResult:m_delegate.appDelegate.nateOAuthResult];
	m_nateRequest.delegate = self;
	m_nateRequest.nateTag = kNateTagCLogProfile;

	NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
	[params setObject:@"json" forKey:@"output"];

	[m_nateRequest requestWithMethodName:CSNateRequestCLogGetProfile
							   andParams:[params autorelease]
						   andOutputType:CSNateRequestOutputTypeJSON];
}


@end
